package smodel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class AbstractModel<T extends NamedModel> implements 
									Model<T>,
									ModelChangedSubject<T>
{
	private List<ModelChangedListener<T>> listeners;
	private T                             state;

	public AbstractModel()
	{
		listeners = Collections.synchronizedList(
				new ArrayList<ModelChangedListener<T>>()
				);
	}
	
	public void addModelChangedListener(ModelChangedListener<T> listener)
	{
		System.out.println("Adding listener...");
		listeners.add(listener);
	}
	
	public T getState()
	{
		return state;
	}
	

	public void notifyListeners(T model)
	{
		for(ModelChangedListener<T> listener: listeners)
			listener.modelChanged(model);
	}
	
	public void removeModelChangedListener(ModelChangedListener<T> listener)
	{
		listeners.remove(listener);
	}
	
	public void setState(T state)
	{
		this.state = state;
		notifyListeners(state);
	}
}
